package com.fdb.efp.cus.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import com.fdb.efp.cus.service.dao.CusWhiteListTecDao;
import com.fdb.efp.cus.service.domain.CusWhiteListTec;
import com.fdb.basic.framework.core.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.efp.cus.service.facade.CusWhiteListTecService;
import com.fdb.efp.cus.service.vo.CusWhiteListTecVO;

/**
 * <p>ServiceImpl: 技术白名单<p>
 * @author ligm
 * @date 2019-04-10
 */
@Service("cusWhiteListTecService")
public class CusWhiteListTecServiceImpl implements CusWhiteListTecService, FrameworkService {

	private static Logger logger = LoggerFactory.getLogger(CusWhiteListTecServiceImpl.class);

	@Autowired
	private CusWhiteListTecDao cusWhiteListTecDao;

	/**
	 * 新增操作
	 */
	@Override
	public int insertCusWhiteListTec(CusWhiteListTecVO inCusWhiteListTecVo) {
		logger.debug("当前新增数据为:"+ inCusWhiteListTecVo.toString());
		int num = 0;
		try {
			CusWhiteListTec cusWhiteListTec = new CusWhiteListTec();
			beanCopy(inCusWhiteListTecVo, cusWhiteListTec);
			num = cusWhiteListTecDao.insertCusWhiteListTec(cusWhiteListTec);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return num;
	}

	/**
	 * 删除操作
	 */
	@Override
	public int deleteByPk(CusWhiteListTecVO inCusWhiteListTecVo) {
		logger.debug("当前删除数据条件为:"+ inCusWhiteListTecVo);
		int num = 0;
		try {
			CusWhiteListTec cusWhiteListTec = new CusWhiteListTec();
			beanCopy(inCusWhiteListTecVo, cusWhiteListTec);
			num = cusWhiteListTecDao.deleteByPk(cusWhiteListTec);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inCusWhiteListTecVo +"删除的数据条数为"+ num);
		return num;
	}


	/**
	 * 更新操作
	 */
	@Override
	public int updateByPk(CusWhiteListTecVO inCusWhiteListTecVo) {
		logger.debug("当前修改数据为:"+ inCusWhiteListTecVo.toString());
		int num = 0;
		try {
			CusWhiteListTec cusWhiteListTec = new CusWhiteListTec();
			beanCopy(inCusWhiteListTecVo, cusWhiteListTec);
			num = cusWhiteListTecDao.updateByPk(cusWhiteListTec);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inCusWhiteListTecVo +"修改的数据条数为"+ num);
		return num;
	}

	/**
	 * 查询操作
	 */
	@Override
	public CusWhiteListTecVO queryByPk(CusWhiteListTecVO inCusWhiteListTecVo) {

		logger.debug("当前查询参数信息为:"+ inCusWhiteListTecVo);
		try {
			CusWhiteListTec queryCusWhiteListTec = new CusWhiteListTec();
			beanCopy(inCusWhiteListTecVo, queryCusWhiteListTec);
			CusWhiteListTec queryRslCusWhiteListTec = cusWhiteListTecDao.queryByPk(queryCusWhiteListTec);
			if (Objects.nonNull(queryRslCusWhiteListTec)) {
				CusWhiteListTecVO outCusWhiteListTecVo = beanCopy(queryRslCusWhiteListTec, new CusWhiteListTecVO());
				logger.debug("当前查询结果为:"+ outCusWhiteListTecVo.toString());
				return outCusWhiteListTecVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}


	/**
	 * 查询用户权限数据
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<CusWhiteListTecVO> queryAllOwner(CusWhiteListTecVO cusWhiteListTecVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<CusWhiteListTecVO> list = null;
		try {
			List<CusWhiteListTec> cusWhiteListTecs = cusWhiteListTecDao.queryAllOwnerByPage(cusWhiteListTecVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ cusWhiteListTecs.size());
			pageSet(cusWhiteListTecs, cusWhiteListTecVo);
			list = (List<CusWhiteListTecVO>) beansCopy(cusWhiteListTecs, CusWhiteListTecVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}


	/**
	 * 查询当前机构权限数据
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<CusWhiteListTecVO> queryAllCurrOrg(CusWhiteListTecVO cusWhiteListTecVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		List<CusWhiteListTec> cusWhiteListTecs = cusWhiteListTecDao.queryAllCurrOrgByPage(cusWhiteListTecVo);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:"+cusWhiteListTecs.size());
		List<CusWhiteListTecVO> list = null;
		try {
			pageSet(cusWhiteListTecs, cusWhiteListTecVo);
			list = (List<CusWhiteListTecVO>) beansCopy(cusWhiteListTecs, CusWhiteListTecVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}


	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<CusWhiteListTecVO> queryAllCurrDownOrg(CusWhiteListTecVO cusWhiteListTecVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		List<CusWhiteListTec> cusWhiteListTecs = cusWhiteListTecDao.queryAllCurrDownOrgByPage(cusWhiteListTecVo);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:"+ cusWhiteListTecs.size());
		List<CusWhiteListTecVO> list = null;
		try {
			pageSet(cusWhiteListTecs, cusWhiteListTecVo);
			list = (List<CusWhiteListTecVO>) beansCopy(cusWhiteListTecs, CusWhiteListTecVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	@Override
	public List<CusWhiteListTecVO> queryByConditions(CusWhiteListTecVO cusWhiteListTecVo) {
		logger.debug("当前查询参数信息为:"+ cusWhiteListTecVo);
		List<CusWhiteListTecVO> list = null;
		try {
			CusWhiteListTec queryCusWhiteListTec = new CusWhiteListTec();
			beanCopy(cusWhiteListTecVo, queryCusWhiteListTec);
			List<CusWhiteListTec> queryRslCusWhiteListTec = cusWhiteListTecDao.queryByConditions(queryCusWhiteListTec);
			if (Objects.nonNull(queryRslCusWhiteListTec)&&!queryRslCusWhiteListTec.isEmpty()) {
				List<CusWhiteListTecVO> outCusWhiteListTecVo = (List<CusWhiteListTecVO>) beansCopy(queryRslCusWhiteListTec, CusWhiteListTecVO.class);
				logger.debug("当前查询结果为:"+ outCusWhiteListTecVo.toString());
				return outCusWhiteListTecVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	@Override
	public CusWhiteListTecVO queryByCertNoAndTyp(CusWhiteListTecVO cusWhiteListTecVo) {
		logger.debug("当前查询参数信息为:"+ cusWhiteListTecVo);
		try {
			CusWhiteListTec queryCusWhiteListTec = new CusWhiteListTec();
			beanCopy(cusWhiteListTecVo, queryCusWhiteListTec);
			CusWhiteListTec queryRslCusWhiteListTec = cusWhiteListTecDao.queryByCertNoAndTyp(queryCusWhiteListTec);
			if (Objects.nonNull(queryRslCusWhiteListTec)) {
				CusWhiteListTecVO outCusWhiteListTecVo = beanCopy(queryRslCusWhiteListTec, new CusWhiteListTecVO());
				logger.debug("当前查询结果为:"+ outCusWhiteListTecVo.toString());
				return outCusWhiteListTecVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	@Override
	public int batchDeleteByPk(CusWhiteListTecVO inCusWhiteListTecVo) {
		logger.debug("当前参数信息为:"+ inCusWhiteListTecVo);
		int num = 0;
		try {
			List<String> cusWhiteIdList = new ArrayList<>();
			if (Objects.nonNull(inCusWhiteListTecVo) && !StringUtil.isNullorBank(inCusWhiteListTecVo.getCusWhiteId())) {
				cusWhiteIdList = Arrays.asList(inCusWhiteListTecVo.getCusWhiteId().split(","));
			}
			num = cusWhiteListTecDao.batchDeleteByPk(cusWhiteIdList);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		return num;
	}

	@Override
	public int batchInsertWhiteList(List<CusWhiteListTecVO> inserCusWhiteListTec) {
		logger.debug("当前新增数据条数为:"+ inserCusWhiteListTec.size());
		int num = 0;
		try {
			num = cusWhiteListTecDao.batchInsertWhiteList(inserCusWhiteListTec);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		return num;
	}

    @Override
    public List<CusWhiteListTecVO> queryByPrdCodeAndCusName(CusWhiteListTecVO cusWhiteListTecVO) throws Exception {
        return (List<CusWhiteListTecVO>) beansCopy(cusWhiteListTecDao.queryByPrdCodeAndCusName(beanCopy(cusWhiteListTecVO, new CusWhiteListTec())), CusWhiteListTecVO.class);
    }
}
